/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    TreeNode *buildTree(vector<int> &num, int h, int t){
        if (h>t) return NULL;
        if (h==t) {
            return new TreeNode(num[h]);
        }
        int mid=(h+t)/2;
        TreeNode *ret=new TreeNode(num[mid]);
        ret->left=buildTree(num, h, mid-1);
        ret->right=buildTree(num, mid+1, t);
        return ret;
    }
public:
    TreeNode *sortedArrayToBST(vector<int> &num) {
        int n=num.size();
        return buildTree(num, 0, n-1);
    }
};
